import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ServletSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取或者创建session对象（服务器中的一块内存空间）
        HttpSession session=request.getSession();

        session.setAttribute("info","我的浏览器在服务器端的空间的编号为："+session.getId());

        //接下来把id以cookie的形式（键：JSESSIONID）返回浏览器
        response.setContentType("text/html;charset=utf-8");
        Cookie cookie=new Cookie("JSESSIONID",session.getId());
        response.addCookie(cookie);
        response.getWriter().write((String) session.getAttribute("info"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
